package com.sjq.common.dao.impl;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.jdbc.support.rowset.SqlRowSet;
import org.springframework.stereotype.Repository;

import com.sjq.common.dao.GenericDao;
import com.sjq.common.dao.MenuTreeDao;
import com.sjq.common.entity.MenuTree;
import com.sjq.common.exception.DataAccessException;

@Repository
public class MenuTreeDaoImpl extends GenericDao<MenuTree> implements MenuTreeDao{

	Logger logger = Logger.getLogger(MenuTreeDaoImpl.class);
	
	@Override
	public List<Map<String, Object>> selectAllJsTree() throws DataAccessException {
		String sql = " select cd_id,cdmc,fcd_id,cdpx from T_PZ_SYS_MENU order by cd_id ";
		List<Map<String, Object>> list = this.getJdbcTemplate().queryForList(sql);
		return list;
	}

	

	@Override
	public MenuTree selectSingleJsTree(String pid) throws DataAccessException {System.out.println("pid:"+pid);
		String sql = "select cd_id,cdmc,fcd_id," +
				"cdpx from T_PZ_SYS_MENU where fcd_id = '"+pid+"' ";
		MenuTree jsTree = new MenuTree();
		 SqlRowSet rs = this.getJdbcTemplate().queryForRowSet(sql);
		 while(rs.next()){
			// System.out.println("-----");
			 jsTree.setMenuId(rs.getString("cd_id"));
			 jsTree.setMenuName(rs.getString("cdmc"));
			 jsTree.setParentMenuId(rs.getString("fcd_id"));
			 jsTree.setOrderno(rs.getInt("cdpx"));
		 }
		 return jsTree;
	}

	@Override
	public List<Map<String, Object>> selectJsTreeChildren() throws DataAccessException {
		String sql = " select * from T_PZ_SYS_MENU where length(cd_id)>3  order by cdpx ";
		 List<Map<String, Object>> list = this.getJdbcTemplate().queryForList(sql);
		return list;
	}

	@Override
	public List<Map<String, Object>> selectTreeByPid(String pid) throws DataAccessException {
		String sql = "select cd_id from T_PZ_SYS_MENU where fcd_id = ? ";
		Object[] params = new Object[]{ pid };
		List<Map<String, Object>> list = this.getJdbcTemplate().queryForList(sql,params);
		return list;
	}

	@Override
	public MenuTree selectSingleMenu(String menuId) throws DataAccessException {
		String sql = "select * from T_PZ_SYS_MENU where cd_id = ? ";
		MenuTree menu = new MenuTree();
		 SqlRowSet rs = this.getJdbcTemplate().queryForRowSet(sql, new Object[]{menuId});
		 while(rs.next()){
			 menu.setMenuId(rs.getString("cd_id"));
			 menu.setMenuName(rs.getString("cdmc"));
			 menu.setMenuIconUrl(rs.getString("wjcclj"));
			 menu.setMenuUrl(rs.getString("cdcs"));
			 menu.setParentMenuId(rs.getString("fcd_id"));
			 menu.setOrderno(rs.getInt("cdpx"));
		 }
		 return menu;
	}

	@Override
	public void insertMenu(MenuTree menu) throws DataAccessException {
		StringBuilder sql =  new StringBuilder()
		        .append(" insert into T_PZ_SYS_MENU ")
		        .append(" (cd_id,cdmc,wjcclj,cdcs,fcd_id,cdpx) ")
		        .append(" values ")
		        .append(" (?,?,?,?,?,?) ");
		Object[] params = new Object[]{
				menu.getMenuId(),
				menu.getMenuName(),
				menu.getMenuIconUrl(),
				menu.getMenuUrl(),
				menu.getParentMenuId(),
				menu.getOrderno()
		};
		logger.debug(sql);
		this.getJdbcTemplate().update(sql.toString(), params);
	}

	@Override
	public void updateMenu(MenuTree menu) throws DataAccessException {
		StringBuilder sql = new StringBuilder()
			        .append(" update T_PZ_SYS_MENU set ")
			        .append(" cdmc = ?, ")
			        .append(" wjcclj = ?, ")
			        .append(" cdcs = ?, ")
			        .append(" cdpx = ? ")
			        .append(" where cd_id = ? ");
		Object[] params = new Object[]{
				menu.getMenuName(),
				menu.getMenuIconUrl(),
				menu.getMenuUrl(),
				menu.getOrderno(),
				menu.getMenuId()
		};
		logger.debug(sql);
		this.getJdbcTemplate().update(sql.toString(), params);
	}

	@Override
	public void deletePrivilege(String menuId) throws DataAccessException {
		StringBuilder sql =  new StringBuilder()
				.append(" delete from t_wf_sys_privilege where resource_id in ")
				.append(" (select tmp.resource_id from  ")
				.append(" (select b.resource_id from t_wf_sys_permission_res b ")
				.append(" where b.object_type = 1 and b.object_id = ?)  tmp) ");
		Object[] params = new Object[]{ menuId };
		this.getJdbcTemplate().update(sql.toString(), params);
	}

	@Override
	public void deletePermission(String menuId) throws DataAccessException {
		String sql = "delete from t_pz_sys_menu where object_type = 1 and object_id = ? ";
		Object[] params = new Object[]{ menuId };
		this.getJdbcTemplate().update(sql, params);
	}

	@Override
	public void deleteMenu(String cdid) throws DataAccessException {
		String sql = "delete from t_pz_sys_menu where cd_id = ? ";
		Object[] params = new Object[]{ cdid };
		this.getJdbcTemplate().update(sql, params);
	}

	@Override
	public List<Map<String, Object>> selectMenuNameByPid(String pid) throws DataAccessException {
		String sql = "select t.cdmc,t.cdsx,t. cd_id,t. cdcs from t_pz_sys_menu t where t.fcd_id = ? "
				         + "and exists (select distinct h.cd_id  from t_pz_sys_role_menu h "
				         + " where h.js_id='0000' "
				         + " and h.cd_id like t.cd_id || '%')"
				         + " order by cdsx, cd_id ";
		Object[] params = new Object[]{ pid };
		List<Map<String, Object>> list = this.getJdbcTemplate().queryForList(sql,params);
		return list;
	}



	@Override
	public int deleteAuthoriedMenu(String authorityId) {
		String sql = "delete T_QXGL_SQXXB where QXZ_DM=? and zyfl='01'";
		return this.getJdbcTemplate().update(sql, new Object[] { authorityId });
	}



	@Override
	public int insertAuthoriedMenu(String menuId, String authorityId) {
		return 	this.getJdbcTemplate().update(
				"insert into T_QXGL_SQXXB (XXZJBH,QXZ_DM,ZY_XXZJBH,ZYFL,DJSJ) values" +
				" (SEQ_QXGLSQXX_XXZJBH.nextval,?,?,?,sysdate)",
				new Object[] {authorityId,menuId,"01" });
	
	}



	@Override
	public List<Map<String, String>> findAuthoriedMenu(String authorityId)
			throws SQLException {
		String sql="select zy_xxzjbh as id from t_qxgl_sqxxb where qxz_dm=? and zyfl='01'";
		return 	this.getListBySql(sql,new Object[] {authorityId});
	}
}
